
public class GeneticCompetition 
{
	private int numberOfVillages;
	private int playersPerVillage;
	private int generationsPerRound;
	private int numberOfRounds;
	
	private Village[] allVillages;
	
	public static void main(String args[])
	{
		// MUST have EVEN number of players per village
		// MUST have NONZERO number of generations per round
		GeneticCompetition gc = new GeneticCompetition(4, 8, 2, 2);
		gc.runCompetition();
		gc.debugMethod();
	}
	
	public GeneticCompetition(int numberOfVillages, int playersPerVillage, int generationsPerRound, int numberOfRounds)
	{
		this.numberOfVillages = numberOfVillages;
		this.playersPerVillage = playersPerVillage;
		this.generationsPerRound = generationsPerRound;
		this.numberOfRounds = numberOfRounds;
		
		allVillages = new Village[numberOfVillages];
		createInitialVillages();
	}
	
	public void runCompetition()
	{

		for(int i = 0; i < numberOfRounds; i++)
		{
			playVillages();
			VillageMixer mixer = new VillageMixer(allVillages);
			allVillages = mixer.returnMixedVillages();
		}
	}
	
	private void createInitialVillages()
	{
		for(int i = 0; i < numberOfVillages; i++)
		{
			Team8_Player[] villagers = new Team8_Player[playersPerVillage];
			
			for(int j = 0; j < playersPerVillage; j++)
			{
				WeightCollection weights = new WeightCollection();
				//Don't really know what to put for player_num. Right now its playersPerVillage%2, which will return 0,1,0,1....
				
				Team8_Player temp;
				//if (j==0)
				//	temp = new Team8_Player(0, new WeightCollection(new int[]{4,3,5,6,10}));
				//else
					temp = new Team8_Player(0,weights);
				
				villagers[j] = temp;
			}
			
			Village tempVillage = new Village(villagers);
			allVillages[i] = tempVillage;
		}
	}
	
	private void playVillages()
	{
		//come back to
		for (int i=0; i<allVillages.length; i++)
		{	try {	
				System.out.println("RUN VILLAGE " + i);
				allVillages[i].runGenerations(generationsPerRound);
			} catch (Exception e) {e.printStackTrace(); }
		}
	}
	
	
	private WeightCollection returnBestPlayersWeights()
	{
		//Player with the highest score
		Team8_Player bestPlayer = null;
		
		for(int i = 0; i < numberOfVillages; i++)
		{
			
			for(int j = 0; j < playersPerVillage; j++)
			{
				if(bestPlayer == null)
				{
					bestPlayer = allVillages[i].returnPlayers()[j];
				}
				else
				{
					if(allVillages[i].returnPlayers()[j].getScore() > bestPlayer.getScore())
					{
						bestPlayer = allVillages[i].returnPlayers()[j];
					}
				}
			}
			
		}
		
		return bestPlayer.getWeights();

	}
	
	public void debugMethod()
	{
		for(int i = 0; i < numberOfVillages; i++)
		{
			System.out.println("Village number " + i + ":");
			for(int j = 0; j < allVillages[i].returnPlayers().length; j++)
			{
				WeightCollection w = allVillages[i].returnPlayers()[j].getWeights();
				System.out.print("Player " + j + " played " +  allVillages[i].returnPlayers()[j].gamesPlayed + " games, scored " + allVillages[i].returnPlayers()[j].getScore());
				System.out.println(" and weights are " + w.getWeight(0) + ", " + w.getWeight(1) + ", " + w.getWeight(2) + ", "
						+ w.getWeight(3) + " " + w.getWeight(4));
			}
			
		}
	}
	
}
